/**
 * Copyright (C), 2015-2020, 真格邦软件科技有限公司
 * 文件名: GeneratorUtil
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/1/31 15:53
 */
package com.zx.utils.generator;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/1/31 15:53
 * 描述: 生成mybatis
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Slf4j
public class GeneratorUtil {

    /**
     * 生成mybatis配置
     *
     * @param path             文件路径
     * @param ip               主机名或IP地址 url
     * @param db               数据库
     * @param port             端口
     * @param username         用户名
     * @param password         密码
     * @param modelpackagename 生成模型的包名
     * @param daopackagename   生成DAO的包名
     * @param mapperpath       生成映射文件的位置
     * @param tablename        表名称
     * @param tablemodel       模型名称
     * @return
     */
    public static void genCode(String path, String ip, String db, String port, String username, String password, String modelpackagename, String daopackagename, String mapperpath, String tablename, String tablemodel) {

        // 清空临时文件夹下所有内容
        try {
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;

            File dirFile = new File(path);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
//            File conf = new File(System.getProperty("user.dir") + "/conf/Configuration.xml");
            File conf = ResourceUtils.getFile("classpath:Configuration.xml");
            ConfigurationParser parser = new ConfigurationParser(warnings);
            Configuration config = parser.parseConfiguration(conf);
            Context context = config.getContexts().get(0);
            // db
            JDBCConnectionConfiguration jdbcConnectionConfiguration = context.getJdbcConnectionConfiguration();
            String connection = "jdbc:mysql://" + ip + ":" + port + "/" + db;
            jdbcConnectionConfiguration.setConnectionURL(connection);
            jdbcConnectionConfiguration.setUserId(username);
            jdbcConnectionConfiguration.setPassword(password);
            // model 配置
            JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
            javaModelGeneratorConfiguration.setTargetPackage(modelpackagename);
            javaModelGeneratorConfiguration.setTargetProject(path);
            // DAO 配置
            JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = context.getJavaClientGeneratorConfiguration();
            javaClientGeneratorConfiguration.setTargetPackage(daopackagename);
            javaClientGeneratorConfiguration.setTargetProject(path);
            // Mapper
            SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = context.getSqlMapGeneratorConfiguration();
            sqlMapGeneratorConfiguration.setTargetPackage(mapperpath);
            sqlMapGeneratorConfiguration.setTargetProject(path);
            // 表
            List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
            tableConfigurations.clear();
            TableConfiguration tableConfiguration = new TableConfiguration(context);
            tableConfiguration.setTableName(tablename);
            tableConfiguration.setDomainObjectName(tablemodel);
            tableConfiguration.setCountByExampleStatementEnabled(false);
            tableConfiguration.setDeleteByExampleStatementEnabled(false);
            tableConfiguration.setSelectByExampleStatementEnabled(false);
            tableConfiguration.setUpdateByExampleStatementEnabled(false);
            tableConfiguration.getProperties().setProperty("useActualColumnNames", "false");
            tableConfigurations.add(tableConfiguration);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成数据异常:{}", e);
        }
    }

}
